﻿using Microsoft.EntityFrameworkCore.Metadata;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using 基于WPF的电缆采样数据分析系统.Models.Enums;
using 基于WPF的电缆采样数据分析系统.Service.Db;

namespace 基于WPF的电缆采样数据分析系统.Models.Dbs.DbService
{

    /// <summary>
    /// 校验码服务
    /// </summary>
    public class InvitationCodeRecordService
    {
        /// <summary>
        /// 单例模型
        /// </summary>
        public static InvitationCodeRecordService Instance = new InvitationCodeRecordService(App.GetService<TDRDbContext>());

        /// <summary>
        /// 数据库上下文
        /// </summary>
        private readonly TDRDbContext db;

        /// <summary>
        /// 构造器
        /// </summary>
        /// <param name="_db"></param>
        public InvitationCodeRecordService(TDRDbContext _db)
        {
            db = _db;
        }


        /// <summary>
        /// 生成邀请码 返回Success则登录成功,返回Fail则登录失败
        /// </summary>
        /// <param name="code">邀请码</param>
        /// <returns></returns>
        public RetResult GenerateInvitationCode(string code)
        {
            try
            {
                InvitationCodeRecord invitationCodeRecord = new InvitationCodeRecord();
                invitationCodeRecord.InvitationCodeInfo = code;
                invitationCodeRecord.InviterId = GlobalVariableEdit.CurrentUserInfo.Uid;
                invitationCodeRecord.GenerationTime = DateTime.Now;
                invitationCodeRecord.IsUsed = 0;

                // 注册邀请码
                db.InvitationCodeRecords.Add(invitationCodeRecord);

                int rows = db.SaveChanges();

                if (rows > 0)
                {
                    return RetResult.Success;
                }

                return RetResult.Fail;
            }
            catch (Exception ex)
            {

                return RetResult.Fail;
            }

        }


        /// <summary>
        /// 邀请码是否存在 0为不存在，1为存在，-1为错误
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public int IsInvitationCodeExist(string code)
        {
            try
            {
                InvitationCodeRecord? invitationCodeRecord = db.InvitationCodeRecords.Where(i => i.InvitationCodeInfo == code).FirstOrDefault();

                if (invitationCodeRecord != null) 
                {
                    return 1;
                }

                return 0;
            }
            catch (Exception ex)
            {

                return -1;
            }
        }


        /// <summary>
        /// 获取邀请码
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public InvitationCodeRecord GetInvitationCodeRecord(string code)
        {
            try
            {
                InvitationCodeRecord? invitationCodeRecord = db.InvitationCodeRecords.Where(i => i.InvitationCodeInfo == code).FirstOrDefault();

                if (invitationCodeRecord != null)
                {
                    return invitationCodeRecord;
                }

                return null;
            }
            catch (Exception ex)
            {

                return null;
            }
        }


        /// <summary>
        /// 修改邀请码的状态 返回Success则登录成功,返回Fail则登录失败
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public RetResult UpdateInvitationCodeRecordStatusAndRegisterId(string code, int RegisterId)
        {
            try
            {
                InvitationCodeRecord? invitationCodeRecord = db.InvitationCodeRecords.Where(i => i.InvitationCodeInfo == code).FirstOrDefault();

                if (invitationCodeRecord != null)
                {
                    invitationCodeRecord.IsUsed = 1;
                    invitationCodeRecord.RegisterId = RegisterId;
                    invitationCodeRecord.RegistrationTime = DateTime.Now;
                    int rows = db.SaveChanges();

                    if (rows > 0)
                    {
                        return RetResult.Success;
                    }
                    return RetResult.Fail;
                }

                return RetResult.Fail;
            }
            catch (Exception ex)
            {

                return RetResult.Fail;
            }
        }


    }
}
